package constant.milk.cashbook.insert;

import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

public class FanTypeActivity extends SuperActivity
{
	public static final String FAN_FLAG = "FAN_TYPE_FLAG";
	public static final String FAN_TYPE_DATA = "FAN_TYPE_DATA";
	
	public static final int TYPE_INSERT = 0;
	public static final int TYPE_RESULT = 1;
	
	private View fanInView;
	private View fanOutView;
	private View fanReturnView;
	private View fanReceiveView;
	
	private RadioButton fanInRadio;
	private RadioButton fanOutRadio;
	private RadioButton fanReturnRadio;
	private RadioButton fanReceiveRadio;
	
	private Button checkButton;
	
	private String typeStr;
	
	private int flag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.fan_type_activity);
		
		setTitle("타입선택");
		
		setGoneTipButton();
		
		setFindViewId();
		setOnClick();
		setData();
	}
	
	@Override
	protected void setFindViewId()
	{
		fanInView = findViewById(R.id.fanTypeInView);
		fanInRadio = (RadioButton) findViewById(R.id.fanTypeInRadio);
		
		fanOutView = findViewById(R.id.fanTypeOutView);
		fanOutRadio = (RadioButton) findViewById(R.id.fanTypeOutRadio);
		
		fanReturnView = findViewById(R.id.fanTypeReturnView);
		fanReturnRadio = (RadioButton) findViewById(R.id.fanTypeReturnRadio);
		
		fanReceiveView = findViewById(R.id.fanTypeReceiveView);
		fanReceiveRadio = (RadioButton) findViewById(R.id.fanTypeReceiveRadio);
		
		checkButton = (Button) findViewById(R.id.checkButton);
	}

	@Override
	protected void setOnClick()
	{
		fanInView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setRadioCheckView(0);
			}
		});
		
		fanOutView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setRadioCheckView(1);
			}
		});
		
		fanReturnView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setRadioCheckView(2);
			}
		});
		
		fanReceiveView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setRadioCheckView(3);
			}
		});
		
		checkButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.putExtra(FAN_TYPE_DATA, typeStr);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}

	@Override
	protected void setData()
	{
		Intent intent = getIntent();
		
		String typeString = intent.getStringExtra(FAN_TYPE_DATA);
		
		flag = intent.getIntExtra(FAN_FLAG, 0);
		
		if (getResources().getString(R.string.fanIn).equals(typeString))
		{
			setRadioCheckView(0);
		}
		else if (getResources().getString(R.string.fanOut).equals(typeString))
		{
			setRadioCheckView(1);
		}
		else if (getResources().getString(R.string.fanReturn).equals(typeString))
		{
			setRadioCheckView(2);
		}
		else if (getResources().getString(R.string.fanReceive).equals(typeString))
		{
			setRadioCheckView(3);
		}
		
		if (flag == TYPE_INSERT)
		{
			fanInView.setVisibility(View.VISIBLE);
			fanOutView.setVisibility(View.VISIBLE);
			fanReturnView.setVisibility(View.GONE);
			fanReceiveView.setVisibility(View.GONE);
		}
		else if (flag == TYPE_RESULT)
		{
			fanInView.setVisibility(View.GONE);
			fanOutView.setVisibility(View.GONE);
			fanReturnView.setVisibility(View.VISIBLE);
			fanReceiveView.setVisibility(View.VISIBLE);
		}
	}
	
	private void setRadioCheckView(int kind)
	{
		if (kind == 0)
		{
			fanInRadio.setChecked(true);
			fanOutRadio.setChecked(false);
			fanReturnRadio.setChecked(false);
			fanReceiveRadio.setChecked(false);
			
			typeStr = getResources().getString(R.string.fanIn);
		}
		else if (kind == 1)
		{
			fanInRadio.setChecked(false);
			fanOutRadio.setChecked(true);
			fanReturnRadio.setChecked(false);
			fanReceiveRadio.setChecked(false);
			
			typeStr = getResources().getString(R.string.fanOut);
		}
		else if (kind == 2)
		{
			fanInRadio.setChecked(false);
			fanOutRadio.setChecked(false);
			fanReturnRadio.setChecked(true);
			fanReceiveRadio.setChecked(false);
			
			typeStr = getResources().getString(R.string.fanReturn);
		}
		else if (kind == 3)
		{
			fanInRadio.setChecked(false);
			fanOutRadio.setChecked(false);
			fanReturnRadio.setChecked(false);
			fanReceiveRadio.setChecked(true);
			
			typeStr = getResources().getString(R.string.fanReceive);
		}
	}
	
	@Override
	protected void tipButton()
	{
	}

}
